local pantan = fk.CreateSkill{
  name = "xiaobai__pantan"
}

Fk:loadTranslationTable{
  ["xiaobai__pantan"] = "叛探",
  [":xiaobai__pantan"] = "出牌阶段限一次，你可以将一张牌当【知己知彼】使用，若因此转化与观看的牌包含："..
    "伤害牌，你摸一张牌；相同牌名，你视为使用【决斗】，若均满足，你变更势力。",
  ["#xiaobai__pantan"] = "叛探：你可以将一张牌当【知己知彼】使用",
  ["#xiaobai__pantan_changeKingdom"] = "叛探：变更势力",

  ["$xiaobai__pantan1"] = "雍闿小儿，岂欲致吾于死地乎！",
  ["$xiaobai__pantan2"] = "来人，去那孔明寨中一探虚实！",
}

pantan:addEffect("active", {
  prompt = "#xiaobai__pantan",
  card_num = 1,
  card_filter = function (self, player, to_select, selected, selected_targets)
    return #selected == 0
  end,
  min_target_num = 0,
  can_use = function (self, player)
    return player:usedSkillTimes(pantan.name) < 1
  end,
  target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
    local to_use = Fk:cloneCard("known_both")
    to_use.skillName = pantan.name
    to_use:addSubcards(selected_cards)
    if not to_use.skill:targetFilter(player, to_select, selected, selected_cards, to_use, Util.DummyTable) then return false end
    if (#selected == 0 or to_use.multiple_targets) and
    player:isProhibited(to_select, to_use) then return false end
    if to_use.multiple_targets then
      if #selected >= player:getHandcardNum() then return false end
      if to_use.skill:getMaxTargetNum(player, to_use) == 1 then
        local x = 0
        for _, p in ipairs(Fk:currentRoom().alive_players) do
          if p.id == to_select or (not player:isProhibited(p, to_use) and to_use.skill:modTargetFilter(player, p, to_select, to_use, true)) then
            x = x + 1
          end
        end
        if x > player:getHandcardNum() then return false end
      end
    end
    return true
  end,
  feasible = function(self, player, selected, selected_cards)
    if #selected_cards == 0 then return false end
    local to_use = Fk:cloneCard("known_both")
    to_use.skillName = pantan.name
    to_use:addSubcards(selected_cards)
    return to_use.skill:feasible(player, selected, selected_cards, to_use)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local card = Fk:cloneCard("known_both")
    card:addSubcards(effect.cards)
    card.skillName = pantan.name
    local currentId = room.logic:getCurrentEvent().id
    local use = { ---@type UseCardDataSpec
      from = player,
      tos = effect.tos,
      card = card,
    }
    room:useCard(use)
    if player.dead then return end
    local card_names = {}
    local same = false
    local view_cards = {effect.cards[1]}
    local is_damage = false
    
    for _, to in ipairs(effect.tos) do
        if #room.logic:getEventsByRule(GameEvent.CardEffect, 1, function (e)
          return e.data.card.trueName == "known_both" and table.contains(e.data.card.skillNames, pantan.name) and (not e.data.isCancellOut) and (not e.interrupted) and (not e.data.nullified)
        end, currentId, Player.HistoryTurn) > 0 then
        table.insertTable(view_cards, to:getCardIds(Player.Hand))
      end
    end
    for _, cid in ipairs(view_cards) do
      if is_damage and same then break end
      local c = Fk:getCardById(cid)
      is_damage = is_damage or c.is_damage_card
      if card_names[c.trueName] then
        same = true
      else
        card_names[c.trueName] = true
      end
    end

    if is_damage then
      player:drawCards(1, pantan.name)
    end
    if player.dead then return end

    if same then
      room:askToUseVirtualCard(player, {
        name = "duel",
        skill_name = pantan.name,
        cancelable = false
      })
    end

    if is_damage and same then
      local all_choices = Fk:getKingdomMap("god")
      local choices = table.simpleClone(all_choices)
      table.removeOne(choices, player.kingdom)
      local prohibitChange = player:getTableMark("@xiaobai__manyi_prohibit_change")
      for _, kd in ipairs(prohibitChange) do
        table.removeOne(choices, kd)
      end
      local choice = room:askToChoice(player, {
        choices = choices,
        skill_name = pantan.name,
        prompt = "#xiaobai__pantan_changeKingdom",
        all_choices = all_choices,
      })

      room:changeKingdom(player, choice, true)
    end
  end,
})

return pantan